﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
using System.IO;
using System.Text.RegularExpressions;
using QuanLyNhapXuat.Presentation.Report;

namespace QuanLyNhapXuat.Presentation.WinForm
{
    public partial class frmThongKeNhapXuat : Telerik.WinControls.UI.RadForm
    {
        //Dùng Singleton
        private static frmThongKeNhapXuat doituong = null;
        public static frmThongKeNhapXuat TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frmThongKeNhapXuat();
            return doituong;
        }
        public frmThongKeNhapXuat()
        {
            InitializeComponent();
        }
        //them service khach hang
        RS_XuLiKhachHang.WS_KhachHangSoapClient sr_kh = new QuanLyNhapXuat.RS_XuLiKhachHang.WS_KhachHangSoapClient();
        //them service chi tiet giao hang
        SR_ChiTietGiaoHang.WS_ChitietgiaohangSoapClient xlctgh = new QuanLyNhapXuat.SR_ChiTietGiaoHang.WS_ChitietgiaohangSoapClient();
        private DataSet ds_kh = new DataSet();
        private DataSet ds_ct = new DataSet();
        private DataSet ds_nam = new DataSet();
        private void frmThongKeNhapXuat_Load(object sender, EventArgs e)
        {
            //Chinh do dai cua cac cot trong muiltcombobox
            RadMultiColumnComboBoxElement multiColumnComboElement = this.mcbb_KhachHang.MultiColumnComboBoxElement;
            multiColumnComboElement.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
            multiColumnComboElement.DropDownMinSize = new Size(300, 200);

            
            //load thong tin khach hang
            try
            {
                ds_kh.ReadXml(new StringReader(sr_kh.layds()));
                mcbb_KhachHang.DataSource = ds_kh.Tables[0];
                mcbb_KhachHang.DisplayMember = "tenkh";
                mcbb_KhachHang.ValueMember = "makh";
                mcbb_KhachHang.SelectedIndex = -1;
                FilterDescriptor des_kh = new FilterDescriptor(this.mcbb_KhachHang.DisplayMember, FilterOperator.StartsWith, string.Empty);
                mcbb_KhachHang.EditorControl.FilterDescriptors.Add(des_kh);
                //load nhung nam da co thong tin
                string cautruyvan = "select distinct YEAR(ngaygiao) as 'nam' from SOGIAO";
                ds_nam.ReadXml(new StringReader(xlctgh.selectquery(cautruyvan)));
                radDDL_nam.DataSource = ds_nam.Tables[0];
                radDDL_nam.DisplayMember = "nam";
                radDDL_year.DataSource = ds_nam.Tables[0];
                radDDL_year.DisplayMember = "nam";
            }
            catch (Exception ex)
            { }
        }

        private void rdbTheoThang_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbTheoThang.Checked == true)
            {
                radDDL_thang.Enabled = true;
                radDDL_nam.Enabled = true;
            }
            else
            {
                radDDL_thang.Enabled = false;
                radDDL_nam.Enabled = false;
            }
        }

        private void rdbTuNgay_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbTuNgay.Checked == true)
            {
                dateTP_TuNgay.Enabled = true;
                dateTP_DenNgay.Enabled = true;
            }
            else
            {
                dateTP_TuNgay.Enabled = false;
                dateTP_DenNgay.Enabled = false;
            }
        }

        private void rdb_TheoNam_CheckedChanged(object sender, EventArgs e)
        {
            if (rdb_TheoNam.Checked == true)
                radDDL_year.Enabled = true;
            else
                radDDL_year.Enabled = false;
        }
        private string cautruyvan;
        private void btn_ThongKeDuLieu_Click(object sender, EventArgs e)
        {
            if (mcbb_KhachHang.SelectedValue == null)
                MessageBox.Show("Bạn hãy chọn thông tin khách hàng !!!");
            else
            {
                int ngaybd;
                int ngaykt;
                int thangbd;
                int thangkt;
                int nambd;
                int namkt;
                int thang;
                int nam;
                int makh = int.Parse(mcbb_KhachHang.SelectedValue.ToString());
                if (rdbTuNgay.Checked == true)
                {
                    try
                    {
                        ds_ct.Clear();
                        ngaybd = dateTP_TuNgay.Value.Day;
                        thangbd = dateTP_TuNgay.Value.Month;
                        nambd = dateTP_TuNgay.Value.Year;

                        ngaykt = dateTP_DenNgay.Value.Day;
                        thangkt = dateTP_DenNgay.Value.Month;
                        namkt = dateTP_DenNgay.Value.Year;

                        cautruyvan = "select tenmh,tenwash,soluongnhap,ngaynhap,soluonggiao,ngaygiao,dongianhap,thanhtien,ghichu "
                                     +"from KHACHHANG,MATHANG,LOAIWASH,SONHAP,CHITIETNHAPHANG,SOGIAO,CHITIETGIAOHANG "
                                     +"where KHACHHANG.MAKH=SONHAP.MAKH and MATHANG.MAMH=CHITIETNHAPHANG.MAMH and MATHANG.MAMH=CHITIETGIAOHANG.MAMH "
                                     +"and LOAIWASH.MAWASH=CHITIETNHAPHANG.MAWASH and LOAIWASH.MAWASH=CHITIETGIAOHANG.MAWASH and SONHAP.MASN=CHITIETNHAPHANG.MASN "
                                     +"and SOGIAO.MASG=CHITIETGIAOHANG.MASG and sogiao.makh='" + makh + "'and DAY(ngaygiao) between " + ngaybd + " and " + ngaykt + " "
                                     +"and MONTH(ngaygiao) between " + thangbd + " and " + thangkt + " and YEAR(NGAYGIAO) between " + nambd + " and " + namkt;                        
                        
                        ds_ct.ReadXml(new StringReader(xlctgh.selectquery(cautruyvan)));
                        DataTable tam_ngay = new DataTable();
                        if (ds_ct.Tables.Count > 0)
                        {
                            gridView_ThongKe.DataSource = null;
                            tam_ngay = ds_ct.Tables[0];
                            for (int i = 0; i < tam_ngay.Rows.Count; i++)
                            {
                                if (tam_ngay.Rows[i][6].ToString().Contains(".0000") == true)
                                {
                                    tam_ngay.Rows[i][6] = tam_ngay.Rows[i][6].ToString().Replace(".0000", "");
                                    tam_ngay.Rows[i]["NgayNhap"] = DateTime.Parse(tam_ngay.Rows[i]["NgayNhap"].ToString()).ToShortDateString();
                                }
                                if (tam_ngay.Rows[i][7].ToString().Contains(".0000") == true)
                                {
                                    tam_ngay.Rows[i][7] = tam_ngay.Rows[i][7].ToString().Replace(".0000", "");
                                    tam_ngay.Rows[i]["NgayGiao"] = DateTime.Parse(tam_ngay.Rows[i]["NgayGiao"].ToString()).ToShortDateString();
                                }
                            }
                            gridView_ThongKe.DataSource = tam_ngay;
                        }
                        else
                        {
                            gridView_ThongKe.DataSource = null;
                            MessageBox.Show("Không Có Dữ Liệu Thống Kê");
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                if (rdbTheoThang.Checked == true)
                {
                    try
                    {
                        ds_ct.Clear();
                        thang = int.Parse(radDDL_thang.Text.ToString());
                        nam = int.Parse(radDDL_nam.Text.ToString());
                        cautruyvan = "select tenmh,tenwash,soluongnhap,ngaynhap,soluonggiao,ngaygiao,dongianhap,thanhtien,ghichu from KHACHHANG,MATHANG,LOAIWASH,SONHAP,CHITIETNHAPHANG,SOGIAO,CHITIETGIAOHANG where KHACHHANG.MAKH=SONHAP.MAKH and MATHANG.MAMH=CHITIETNHAPHANG.MAMH and MATHANG.MAMH=CHITIETGIAOHANG.MAMH and LOAIWASH.MAWASH=CHITIETNHAPHANG.MAWASH and LOAIWASH.MAWASH=CHITIETGIAOHANG.MAWASH and SONHAP.MASN=CHITIETNHAPHANG.MASN and SOGIAO.MASG=CHITIETGIAOHANG.MASG and sogiao.makh='" + makh + "' and MONTH(ngaygiao)=" + thang + " and YEAR(NGAYGIAO)=" + nam;
                        ds_ct.ReadXml(new StringReader(xlctgh.selectquery(cautruyvan)));
                        DataTable tam_thang = new DataTable();
                        if (ds_ct.Tables.Count > 0)
                        {
                            gridView_ThongKe.DataSource = null;
                            tam_thang = ds_ct.Tables[0];
                            for (int i = 0; i < tam_thang.Rows.Count; i++)
                            {
                                if (tam_thang.Rows[i][6].ToString().Contains(".0000") == true)
                                {
                                    tam_thang.Rows[i][6] = tam_thang.Rows[i][6].ToString().Replace(".0000", "");
                                    tam_thang.Rows[i]["NgayNhap"] = DateTime.Parse(tam_thang.Rows[i]["NgayNhap"].ToString()).ToShortDateString();
                                }
                                if (tam_thang.Rows[i][7].ToString().Contains(".0000") == true)
                                {
                                    tam_thang.Rows[i][7] = tam_thang.Rows[i][7].ToString().Replace(".0000", "");
                                    tam_thang.Rows[i]["NgayGiao"] = DateTime.Parse(tam_thang.Rows[i]["NgayGiao"].ToString()).ToShortDateString();
                                }
                            }
                            gridView_ThongKe.DataSource = tam_thang;
                        }
                        else
                        {
                            gridView_ThongKe.DataSource = null;
                            MessageBox.Show("Không Có Dữ Liệu Thống Kê");
                        }
                    }
                    catch (Exception ex)
                    { }
                }
                if (rdb_TheoNam.Checked == true)
                {
                    try
                    {
                        ds_ct.Clear();
                        nam = int.Parse(radDDL_year.Text);
                        cautruyvan = "select tenmh,tenwash,soluongnhap,ngaynhap,soluonggiao,ngaygiao,dongianhap,thanhtien,ghichu from KHACHHANG,MATHANG,LOAIWASH,SONHAP,CHITIETNHAPHANG,SOGIAO,CHITIETGIAOHANG where KHACHHANG.MAKH=SONHAP.MAKH and MATHANG.MAMH=CHITIETNHAPHANG.MAMH and MATHANG.MAMH=CHITIETGIAOHANG.MAMH and LOAIWASH.MAWASH=CHITIETNHAPHANG.MAWASH and LOAIWASH.MAWASH=CHITIETGIAOHANG.MAWASH and SONHAP.MASN=CHITIETNHAPHANG.MASN and SOGIAO.MASG=CHITIETGIAOHANG.MASG and sogiao.makh='" + makh + "' and YEAR(NGAYGIAO)=" + nam;
                        ds_ct.ReadXml(new StringReader(xlctgh.selectquery(cautruyvan)));
                        DataTable tam_nam = new DataTable();
                        if (ds_ct.Tables.Count > 0)
                        {
                            gridView_ThongKe.DataSource = null;
                            tam_nam = ds_ct.Tables[0];
                            for (int i = 0; i < tam_nam.Rows.Count; i++)
                            {
                                if (tam_nam.Rows[i][6].ToString().Contains(".0000") == true)
                                {
                                    tam_nam.Rows[i][6] = tam_nam.Rows[i][6].ToString().Replace(".0000", "");
                                    tam_nam.Rows[i]["NgayNhap"] = DateTime.Parse(tam_nam.Rows[i]["NgayNhap"].ToString()).ToShortDateString();
                                }
                                if (tam_nam.Rows[i][7].ToString().Contains(".0000") == true)
                                {
                                    tam_nam.Rows[i][7] = tam_nam.Rows[i][7].ToString().Replace(".0000", "");
                                    tam_nam.Rows[i]["NgayGiao"] = DateTime.Parse(tam_nam.Rows[i]["NgayGiao"].ToString()).ToShortDateString();
                                }
                            }
                            gridView_ThongKe.DataSource = tam_nam;
                        }
                        else
                        {
                            gridView_ThongKe.DataSource = null;
                            MessageBox.Show("Không Có Dữ Liệu Thống Kê");
                        }
                    }
                    catch (Exception ex)
                    { }
                }
            }
        }

        private void radDDL_thang_Leave(object sender, EventArgs e)
        {
            
        }

        private void btn_InThongTin_Click(object sender, EventArgs e)
        {
            frm_reportNhapXuat report = new frm_reportNhapXuat();
            report.makh = mcbb_KhachHang.SelectedValue.ToString();
            report.Show();
        }

        private void btn_Dong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
